package br.com.advance.automacao.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.springframework.stereotype.Repository;

import br.com.advance.automacao.dao.TurnoDao;
import br.com.advance.automacao.generic.dao.impl.GenericDaoImpl;
import br.com.advance.automacao.model.Turno;

@Repository("turnoDao")
public class TurnoDaoImpl extends GenericDaoImpl<Turno, Serializable> implements TurnoDao {

	@Override
	public Turno buscarTurnoPeloHorario(Date date) {
		Query query = entityManager.createNamedQuery(Turno.BUSCAR_TURNO_PELO_HORARIO);
		query.setParameter("horaAtual", date, TemporalType.TIME);
		return (Turno) query.getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turno> buscarTurnosPelaDataCLP(Date data, Long idCLP) {
		Query query = entityManager.createNamedQuery(Turno.BUSCAR_TURNO_PELA_DATA_CLP);
		query.setParameter("data", data, TemporalType.DATE);
		query.setParameter("idCLP", idCLP);
		return query.getResultList();
	}

	@Override
	public Turno buscarPorCodigo(Integer codigo) {
		Query query = entityManager.createNamedQuery(Turno.BUSCAR_POR_CODIGO);
		query.setParameter("codigo", codigo);
		try {
			return (Turno) query.getSingleResult();
		} catch(NoResultException e) {
			return null;
		}
	}

}
